package com.estore.model;

import java.io.Serializable;
import java.util.*;

public class Product implements Serializable {
    private static final long serialVersionUID = 1L;

    // 字段添加final修饰符（如果业务允许）
    private final String id;       // 商品ID
    private String name;     // 商品名称
    private double price;    // 价格
    private int stock;       // 库存
    private Set<String> tags; // 标签

    // 添加参数验证
    public Product(String id, String name, double price, int stock) {
        this(id, name, price, stock, new HashSet<>());
    }

    public Product(String id, String name, double price, int stock, Set<String> tags) {
        this.id = Objects.requireNonNull(id, "Product ID cannot be null");
        this.name = Objects.requireNonNull(name, "Product name cannot be null");
        setPrice(price);  // 使用setter进行验证
        setStock(stock);
        this.tags = new HashSet<>(Objects.requireNonNull(tags, "Tags cannot be null"));
    }

    // Getters and Setters...
    public String getId() { return id; }
    // 移除setId或者保持，根据业务需求

    public String getName() { return name; }
    public void setName(String name) {
        this.name = Objects.requireNonNull(name, "Product name cannot be null");
    }

    public double getPrice() { return price; }
    public void setPrice(double price) {
        if (price < 0) {
            throw new IllegalArgumentException("Price cannot be negative");
        }
        this.price = price;
    }

    public int getStock() { return stock; }
    public void setStock(int stock) {
        if (stock < 0) {
            throw new IllegalArgumentException("Stock cannot be negative");
        }
        this.stock = stock;
    }

    // Tags相关方法
    public Set<String> getTags() {
        return Collections.unmodifiableSet(tags);
    }

    public void setTags(Set<String> tags) {
        this.tags = new HashSet<>(Objects.requireNonNull(tags, "Tags cannot be null"));
    }

    // 添加便捷方法操作tags
    public void addTag(String tag) {
        Objects.requireNonNull(tag, "Tag cannot be null");
        this.tags.add(tag);
    }

    public void removeTag(String tag) {
        this.tags.remove(tag);
    }

    public boolean hasTag(String tag) {
        return this.tags.contains(tag);
    }

    @Override
    public String toString() {
        return String.format("ID: %-5s | Name: %-20s | Price: $%-8.2f | Stock: %d | Tags: %s",
                id, name, price, stock, tags);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Product)) return false;
        Product product = (Product) o;
        return Objects.equals(id, product.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    // 添加builder模式（可选）
    public static Builder builder(String id, String name) {
        return new Builder(id, name);
    }

    public static class Builder {
        private final String id;
        private final String name;
        private double price;
        private int stock;
        private Set<String> tags = new HashSet<>();

        public Builder(String id, String name) {
            this.id = Objects.requireNonNull(id);
            this.name = Objects.requireNonNull(name);
        }

        public Builder price(double price) {
            this.price = price;
            return this;
        }

        public Builder stock(int stock) {
            this.stock = stock;
            return this;
        }

        public Builder tags(Set<String> tags) {
            this.tags = new HashSet<>(tags);
            return this;
        }

        public Builder addTag(String tag) {
            this.tags.add(tag);
            return this;
        }

        public Product build() {
            return new Product(id, name, price, stock, tags);
        }
    }
}